<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/layouts/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>创建文章</title>
    <%@include file="/WEB-INF/layouts/edit-header.jsp" %>

    <%--select--%>
    <link rel="stylesheet" type="text/css" href="${staticPath}/select/css/bootstrap-drilldown-select.css">
    <%--select--%>

    <%--select2--%>
    <link href="${staticPath}/select2/dist/css/select2.min.css" rel="stylesheet">
    <%--select2--%>

    <script type="text/javascript">
        var _ctx = '${ctx}';
        var _staticPath = '${staticPath}';
    </script>

    <%--neditor--%>
    <script type="text/javascript" charset="utf-8" src="${staticPath}/neditor/neditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${staticPath}/neditor/_examples/editor_api.js"></script>
    <script type="text/javascript" charset="utf-8" src="${staticPath}/neditor/lang/zh-cn/zh-cn.js"></script>

    <%--select--%>
    <script src="${staticPath}/select/js/bootstrap-drilldown-select.js"></script>
    <script src="${staticPath}/admin/tcsf/wg_listToTree1.1.js"></script>
    <%--select--%>

    <%--select2--%>
    <script src="${staticPath}/select2/dist/js/select2.min.js"></script>
    <script src="${staticPath}/select2/dist/js/i18n/zh-CN.js"></script>

    <style type="text/css">
        div {
            width: 100%;
        }

        .myCheckBox {
            margin-top: 10px !important;
        }

        .select2-selection__choice {
            background-color: white !important;
            border-radius: 0px !important;
        }

        .select2-dropdown {
            z-index: 99999999999 !important;
        }

        .dropdown-menu {
            z-index: 99999999999 !important;
        }

        .select2-selection {
            background-color: #FFF !important;
            border: 1px solid #e5e6e7 !important;
            border-radius: 1px !important;
        }

        .select2-search__field {
            margin-left: 5px;
            margin-top: 0px!important;
        }
    </style>
</head>
<body>
<div class="tpanel">
    <div class="panel-content">
        <div class="container-fluid">
            <div class="ibox-content" style="padding-top:50px;">
                <form id="createForm" class="form-horizontal">

                    <div class="form-group form-group-first">
                        <label for="title" class="col-sm-2 control-label">文章标题</label>
                        <div class="col-sm-8 ">
                            <input type="text" class="form-control" id="title" name="title"
                                   placeholder="请输入文章标题" required maxlength="20"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="summary" class="col-sm-2 control-label">文章摘要</label>
                        <div class="col-sm-8 ">
                            <textarea class="form-control" name="summary" id="summary" required
                                      placeholder="请输入文章摘要" onfocus="adjustHeight(1)" onblur="adjustHeight(-1)"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="editor" class="col-sm-2 control-label">文章内容</label>
                        <div class="col-sm-8 ">
                            <div name="content" id="editor" style="height:300px;"></div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="keyword" class="col-sm-2 control-label">文章标签</label>
                        <div class="col-sm-8 ">
                            <select class="form-control js-example-tags" multiple="multiple" name="keyword" id="keyword">
                                <c:forEach var="label" items="${labels}">
                                    <option>${label.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">文章分类</label>
                        <div class="col-sm-8 ">
                            <input id="drilldown1" class="form-control" placeholder="请选择分类"
                                   data-toggle="dropdown" readonly="readonly">
                            <input id="parentId" class="hidden" name="catalogId">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="isShow" class="col-sm-2 control-label">是否对所有人可见</label>
                        <div class="col-sm-8">
                            <input class="myCheckBox" type="checkbox" name="isShow" id="isShow">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="isComment" class="col-sm-2 control-label">是否允许评论</label>
                        <div class="col-sm-8">
                            <input class="myCheckBox" type="checkbox" name="isComment" id="isComment">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="isTop" class="col-sm-2 control-label">是否置顶</label>
                        <div class="col-sm-8">
                            <input class="myCheckBox" type="checkbox" name="isTop" id="isTop">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 " style="text-align: right;">
                            <%--<a style="color:#5cb85c; font-size: 16px;text-decoration: none">保存草稿</a>--%>
                        </div>
                        <div class="col-sm-8 " style="text-align: right;">
                            <button type="submit" class="btn btn-success">发布文章</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/javascript">
    changeTitle('发布文章');
    var ue;
    $(function () {

        initForm('#createForm', '${ctx}/admin/cms/article/create', function (r) {
            var resp = $.parseJSON(r);
            if (resp.code == 'OK') {
                changeUrl('${ctx}/admin/cms/article');
                topLayer.msg('保存成功', {icon: 1});
            } else {
                topLayer.msg(resp.message, {icon: 2});
            }
        });

        ue = UE.getEditor('editor', {
            serverUrl: _ctx + "/admin/cms/article/editor",
            initialContent: '请输入文章内容',
            elementPathEnabled: false
        });

        $(".js-example-tags").select2({
            tags: true,
            tokenSeparators: [',', '，'],
            maximumSelectionLength: 5,
            language: 'zh-CN',
            placeholder: '请选择五个关键词',
        });

        $('.select2-search__field').addClass("form-control");

        $.getJSON('${ctx}/admin/cms/article-catalog/list', function (result) {
            if (result.code == 'OK') {
                fillData(result.body);
            }
        });
    })

    function adjustHeight(val) {
        if (val == 1) {
            $('#abstract').height(200);
        } else {
            $('#abstract').height(54);
        }
    }

    function fillData(data) {
        var param = {
            list: data,
            inParams: {
                pid: "parentId",
                rootId: null,
                id: 'id',
                name: 'catalogName',
            },
            outParams: {
                children: "list",
                pid: "pid",
                id: 'id',
                name: 'catalogName',
            },
            sort: {
                orderBy: ["sort"],
                sort: "asc"
            }
        };
        var tree = wg_listToTree(param);
        if (tree.length == 0) {
            tree = [{
                id: -1,
                catalogName: "没有分类，去创建一个吧",
            }]
        }
        $('#drilldown1').drilldownSelect({
            appendValue: false,
            data: tree,
            valueName: 'catalogName',
            onSelected: function (event) {
                if ($(event.target).data('id') === -1) {
                    window.location.href = '${ctx}/admin/cms/article-catalog';
                }
                $('#parentId').val($(event.target).data('id'));
                $('#drilldown1').val(event.target.innerHTML);
            }
        });
    }

</script>
</html>
